/**
 * 班组信息管理
 */
Ext.define('Module.jc.JcBz', {
	extend : 'Ext.panel.Panel',
	alias : 'widget.jcBz',
	layout: 'border',
	config:{
		bzzyMajorId:''
	},
	initComponent : function() {
		var me = this;
		/**
		 * 班组
		 */
		var store=Ext.create('Ext.data.Store',{
			autoLoad: false,
			fields: ['id','zytype','zytypename','bzCode','bzName','upuser','uptime','name','bdzName','bzzyName','bzzyCode','jcBdzId','bzzyMajorId'],
	        proxy: {
	            type: 'ajax',
	            url: 'jc/JcBzAction/getJcBz.do',
	            reader: {
					root: 'datas',
					totalProperty: 'total'
				}
	        }
		});
		/**
		 * 专业信息
		 */
		var bzzyMajorstore=Ext.create('Ext.data.Store',{
			autoLoad: true,
			fields: ['id','bzzyName'],
	        proxy: {
	            type: 'ajax',
	            url: 'bzzy/BzzyZygcAction/getBzzyMajor.do'
	        }
		});
		
		
		var bzzyMajorPanel = me.bzzyMajorPanel = Ext.create('Ext.grid.Panel',{
			title: '专业',
			region: 'west',
			padding: '0 1 0 0',
			width:280,
			store: bzzyMajorstore,
			tbar:[{
				xtype: 'panel',
				border: false,
				height: 25,
				width: 280,
				bodyPadding: 0-1,
				layout: 'column',
				defaults: {columnWidth: 1,bodyPadding: 0},
				items: [{
						xtype: 'toolbar',
						items: [{
							xtype:"textfield",
							fieldLabel : '专业名称',	
							maxLength:40,
							labelWidth:60,
							width:180
						},{
							text:"查询",
							iconCls: 'icon-search',
							handler: function(){
								var tbar = this.up('toolbar');
								var items = tbar.items.items;

								bzzyMajorPanel.store.proxy.extraParams = {
											bzzyName: items[0].getSubmitValue()
									};
								bzzyMajorPanel.store.load();
							}
						}
					]
				 }]
			}],
			columns: [{
	            flex: 2,
	            dataIndex: 'bzzyName'
			}],
			listeners:{
				itemclick:function(tree, record, item, index, e, eOpts) { 
					if(record.data.id != me.majoroid){
						me.majoroid = record.data.id;
						var stool = me.jcBz.down('toolbar').items.items[0].items.items[0].items.items;
						stool[3].setValue();
						stool[4].setValue();
						store.proxy.extraParams = {
								bzzyMajorId:record.data.id
						};
						store.load();
					}
				}
			}
			
		});
		
		
		var jcBz = me.jcBz = Ext.create('Ext.grid.Panel',{
			title: '班组信息列表',
			region: 'center',
			padding: '0 1 0 0',
			store: store,
			tbar:[{
				xtype: 'panel',
				border: false,
				height: 25,
				width: '100%',
				bodyPadding: 0-1,
				layout: 'column',
				defaults: {columnWidth: 1,bodyPadding: 0},
				items: [{
						xtype: 'toolbar',
						items: [{
							text: '添加信息',
							iconCls: 'icon-add',
							handler: function(){
								var rec = bzzyMajorPanel.getSelectionModel().getSelection()[0];
								if(rec){
									me.addJcBz();
									Ext.getCmp(me.id+'bzzyName').setValue(rec.data.bzzyName);
									Ext.getCmp(me.id+'bzzyMajorId').setValue(rec.data.id);
								}
								else{
									Ext.Msg.alert('提示', '请选择要添加的专业数据！');
								}
							}
						},{
							text: '信息',
							iconCls: 'icon-edit',
							handler: function(){
								var rec = jcBz.getSelectionModel().getSelection()[0];
								if(rec){
									me.addJcBz(rec);
								}
								else{
									Ext.Msg.alert('提示', '请选择要修改的信息！');
								}
							}
						},{
							text: '删除信息',
							iconCls: 'icon-delete',
							handler: function(){
								var rec = jcBz.getSelectionModel().getSelection()[0];
								if(rec){
									me.delJcBz(rec);
								}
								else{
									Ext.Msg.alert('提示', '请选择要删除的信息！');
								}
							}
						},{
							xtype:"textfield",
							fieldLabel : '班组名称',	
							maxLength:40,
							labelWidth:60,
							width:190
						},{
							xtype:"textfield",
							fieldLabel : '班组编码',	
							maxLength:20,
							labelWidth:60,
							width:190
						},{
							text:"查询",
							iconCls: 'icon-search',
							handler: function(){
								var tbar = this.up('toolbar');
								var items = tbar.items.items;
								var MajorId = me.bzzyMajorPanel.getSelectionModel().getSelection()[0];
								
								
								if(MajorId){
									jcBz.store.proxy.extraParams = {
											bzName: items[3].getSubmitValue(),
											bzCode: items[4].getSubmitValue(),
											bzzyMajorId: MajorId.data.id
									};
									jcBz.store.loadPage(1);
								}
							}
						}
					]
					 }]
			}],
			bbar: Ext.create('Ext.PagingToolbar', {
	            store: store,
	            id: me.id+'-bbar',
	            displayInfo: true,
	            displayMsg: '显示第 {0} 至 {1} 条记录，共 {2} 条',
	            emptyMsg: "无记录"
	        }),
			columns: [{
	            text: '班组名称',
	            flex: 2,
	            dataIndex: 'bzName'
			},{
	            text: '班组编码',
	            flex: 1.5,
	            dataIndex: 'bzCode'
			},{
	            text: '更新用户',
	            flex: 2,
	            dataIndex: 'name'
			},{
	            text: '更新时间',
	            flex: 2,
	            dataIndex: 'uptime',
	            renderer: formatDate
			}]
			
		});
		
		
		me.items = [jcBz,bzzyMajorPanel];
		
		me.callParent();
	},
	addJcBz: function(rec){
		var me = this;
		Ext.create('Ext.window.Window',{
			width: 410,
			height: 210,
			iconCls: 'icon-add',
			modal: true,
			layout: 'fit',
			title: '添加信息',
			buttons: [{
				text: '保存',
				handler: function(){
					var formp = this.up('panel').items.items[0];
        	    	var form = formp.getForm();
        	    	if(form.isValid()){
        	    		form.submit({
            	    	    url: 'jc/JcBzAction/addJcBz.do',
            	    	    success: function(form, action) {
            	    	    	var rs = Ext.decode(action.response.responseText);
            	    	    	if(rs.rst){
            	    	    		Ext.Msg.alert('提示', '保存成功！');
            	    	    		formp.up('window').close();
            	    	    		me.jcBz.store.reload();
            	    	    	}
            	    	    	else{
            	    	    		Ext.Msg.alert('提示', '保存失败！详情：'+rs.msg);
            	    	    	}
            	    	    }
            	    	});
        	    	}
        	    	else{
        	    		Ext.Msg.alert('提示', '无效的信息,请正确填写信息！');
        	    	}
				}
			},{
				text: '取消',
				handler: function(){
					this.up('window').close();
				}
			}],
			items: me.getForm({
				bodyPadding: 20,
				border: false,
				listeners: {
					afterrender: function(fp){
						fp.loadRecord(rec||{});
					}
				}
			})
		}).show();
	},
	delJcBz: function(rec){
		var me = this;
		if(rec){
			Ext.Msg.confirm('提示', '你确定要删除班组信息吗？',function(b){
				if('yes' == b){
					Ext.Ajax.request({
					    url: 'jc/JcBzAction/deleteJcBz.do',
					    params: rec.data,
					    success: function(response){
        	    	    	var rs = Ext.decode(response.responseText);
        	    	    	if(rs.rst){
        	    	    		Ext.Msg.alert('提示', '删除成功！');
        	    	    		me.jcBz.store.reload();
        	    	    	}
        	    	    	else{
        	    	    		Ext.Msg.alert('提示', '删除失败！详情：'+rs.msg);
        	    	    	}
					    }
					});
				}
			});
		}
		else{
			Ext.Msg.alert('提示', '请选择要删除的信息！');
		}
	},
	getForm: function(config,readOnly){
		var me=this;
		return Ext.apply({
			xtype: 'form',
			bodyPadding: 10,
			defaults: {anchor: '100%'},
			items: [{
					name: 'id',
					xtype: 'hidden'
				},{
					name: 'bzzyMajorId',
					xtype: 'hidden',
					id: me.id+'bzzyMajorId'
				},{
					xtype: 'textfield',
					fieldLabel: '所属专业',
					readOnly:true,
					name:'bzzyName',
					id:me.id+'bzzyName'
				},{
					fieldLabel: '班组名称',
					name: 'bzName',
					maxLength:40,
					xtype: 'textfield',
					allowBlank: false
				},{
					fieldLabel: '班组编码',
					name: 'bzCode',
					maxLength:50,
					xtype: 'textfield',
					allowBlank: false
				}
			]
		},config);
	}
});